import os
import sys
import xml.etree.ElementTree as ET
import glob


def xml2txt(indir, outdir):
    os.chdir(indir)
    annotations = os.listdir('.')
    print('1', annotations)
    annotations = glob.glob(str(annotations) + '*xml')
    print('2',annotations)
    for i, file in enumerate(annotations):
        file_save = file.split('.')[0] + '.txt'
        file_txt = os.path.join(outdir, file_save)
        f_w = open(file_txt, 'w')

        in_file = open(file)
        tree = ET.parse(in_file)
        root = tree.getroot()

        for obj in root.iter('object'):
            current = list()
            name = obj.find('name').text

            xmlbox = obj.find('bndbox')
            xn = xmlbox.find('xmin').text
            xx = xmlbox.find('xmax').text
            yn = xmlbox.find('ymin').text
            yx = xmlbox.find('ymax').text
            print(type(xn))

            f_w.write(xn + ' ' + yn + ' ' + xx + ' ' + yx + ' ')


            f_w.write(name + '\n')

indir='F:/data/label/'
outdir='F:/data/label_txt/'
xml2txt(indir,outdir)